package cn.ezios.huo.system.biz.controller;

import cn.ezios.huo.system.dao.domain.SysDictOption;
import cn.ezios.huo.system.dao.dto.SysDictOptionDTO;
import cn.ezios.huo.system.dao.vo.SysDictOptionVO;
import cn.ezios.huo.system.dao.service.SysDictOptionService;
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sys/dict/option")
@RequiredArgsConstructor
public class SysDictOptionController {

    private final SysDictOptionService sysDictOptionService;

    @PostMapping("")
    public Ret add(@RequestBody SysDictOption sysDictOption) {
        boolean save = sysDictOptionService.save(sysDictOption);
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody SysDictOption sysDictOption) {
        boolean update = sysDictOptionService.updateById(sysDictOption);
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        SysDictOption sysDictOption = sysDictOptionService.getById(id);
        return sysDictOption != null ? Ret.ok(sysDictOption) : Ret.error("未找到");
    }

    @GetMapping("page")
    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
    @RequestParam(value = "size", defaultValue = "10") Integer size,SysDictOptionDTO dto) {
       IPage <SysDictOptionVO> page = sysDictOptionService.queryPage(current,size,dto);
            return Ret.ok(page);
    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
        boolean remove = sysDictOptionService.removeById(id);
        return remove ? Ret.ok() : Ret.error();
    }

    // 根据code查询字典项列表
    @GetMapping("listByCode/{code}")
    public Ret listByCode(@PathVariable("code") String code) {
        return Ret.ok(sysDictOptionService.listByCode(code));
    }
}
